<?php
// +----------------------------------------------------------------------
// | Iecms [ Make the most exquisite CMS ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009~2019 http://www.iemoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: iemoo <i@iemoo.com>
// +----------------------------------------------------------------------
// | DateTime: 2019/11/19 11:08
// +----------------------------------------------------------------------
namespace app\api\controller;

use app\common\model\Files as FilesModel;

use app\admin\validate\Files as FilesValidate;
use think\exception\ValidateException;

use app\BaseController;
use think\facade\Config;
use think\facade\Request;


//------------------------
// 文件上传接口
//-------------------------

class Upload extends BaseController
{

	/*
	* 上传文件
	* @param string			$dir		文件保存目录
	* @param string			$allowext	上传文件验证
	* @param string			$purpose	初次上传用途
	* @param boole			$thumb		是否生成缩略图
	*/

	public function index($dir='image',$allowext='jpg,gif,jpeg,png',$purpose='archives',$thumb = 'true'){

		$file = Request::file('file');

		//根据MD5判断服务器是否已有此图片
		$uploads = FilesModel::where('md5',$file->md5())->findOrEmpty();
		
		//如果已有，直接返回结果
		if (!$uploads->isEmpty()) {	
			$result = [
                'code'	=> 1,
                'id'	=> $uploads->id,
				'name'	=> $file->getOriginalName(),
				'url'	=> $uploads->url
            ];
			if($thumb == 'true'){
				$result['thumb'] =  get_thumb($uploads->url,200,200);
			}
			return json($result);
		}

		try {

			validate(['file'=>'fileSize:2048000|fileExt:'.$allowext])->check(['file' => $file]);


			$savename = \think\facade\Filesystem::disk('public')->putFile($dir, $file);
		
			$url = Config::get('filesystem.disks.public.url').'/'.str_replace('\\', '/', $savename);
			

			$uploads->md5 = $file->md5();
			$uploads->ext = $file->extension();
			$uploads->size = is_file($file) ? filesize($file) : 0;
			$uploads->url = $url;
			$uploads->purpose = $purpose;
			$uploads->save();
	

			$result = [
                'code'	=> 1,
                'id'	=> $uploads->id,
				'name'	=> $file->getOriginalName(),
				'url'	=> $uploads->url
            ];

			if($thumb == 'true'){
				$result['thumb'] =  get_thumb($uploads->url,200,200);
			}


		} catch (think\exception\ValidateException $e) {
			$result = [
                'code'   => 0,
                'message' => $e->getMessage()
            ];
		}
		return json($result);
	}


	/*
	* 编辑器多图上传	WangEditer
	* @param files		$files		文件
	*/
	public function wangEditer(){

		$files = Request::file();

		$data = [];
		
		try {
			
			foreach($files as $file){

				//根据MD5判断服务器是否已有此图片
				$uploads = FilesModel::where('md5',$file->md5())->findOrEmpty();
				
				//如果已有，返回结果
				if (!$uploads->isEmpty()) {	

					$data[]	= $uploads->url;

				}else{

					validate(['file'=>'fileSize:2048000|fileExt:jpg,gif,jpeg,png'])->check(['file'=>$file]);

					$savename = \think\facade\Filesystem::disk('public')->putFile('editor', $file);
					$url = Config::get('filesystem.disks.public.url').'/'.str_replace('\\', '/', $savename);
					
					//保存文件记录
					$uploads->md5 = $file->md5();
					$uploads->ext = $file->extension();
					$uploads->size = is_file($file) ? filesize($file) : 0;
					$uploads->url = $url;
					$uploads->purpose = 'editor';
					$uploads->save();

					$data[] = $url;

				}
			}

			$result = [
                'errno' => 0,
                'data'   => $data
            ];

		} catch (think\exception\ValidateException $e) {
			$result = [
                'errno'   => 1,
                'message' => $e->getMessage()
            ];
		}

		return json($result);
	}


}
